/**
 * 
 */
package com.sysmanage.etl.controls;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sysmanage.etl.model.ETL_ErrorLog;
import com.sysmanage.etl.model.ETL_Excel;
import com.sysmanage.etl.service.IErrorLogService;
import com.sysmanage.etl.service.IImportDataService;
import com.sysmanage.etl.vo.ErrorLogVO;

/**
 * @author Yu Jou 2014年7月21日
 */

@Scope("prototype")
@Controller(value = "errorLogTg")
public class ErrorLogTg extends BaseETLTg{
	private static final long serialVersionUID = -1808357032212318040L;
	
	@Resource
	private IErrorLogService errorLogService;
	@Resource
	private IImportDataService importService;

	private List<ErrorLogVO> errorList = new ArrayList<ErrorLogVO>();
	private Map<String, Object> result = new HashMap<String, Object>();
	private Integer excelId;
	private Integer errorLogId;
	
	public String index(){
		return "index";
	}
	
	public String rexcuteError() {
		return "updateJSON";
	}
	
	/**
	 * 
	 */
	public void prepareRexcuteError() {
		try {
			ETL_ErrorLog errorLog = errorLogService.findById(errorLogId);
			Integer error = importService.saveData(errorLog.getExcel(), errorLog.getExcelColumn_Index());
			String msg = "";
			switch (error) {
			case -99 : msg = "文件不存在，请重新上传后再执行";break;
			case -1 : msg = "导入时发生异常，请稍后再试";break;
			default : msg = "共提取1条记录，其中失败" + result + "条记录";
			}
			errorLog.setStatus(1);
			errorLogService.alter(errorLog);
			result.put("msg", msg);
			result.put("success", true);
		} catch (Exception e) {
			result.put("msg", "操作失败!");
			result.put("success", false);
			e.printStackTrace();
		}
	}
	
	public String rexcuteAllError() {
		return "updateJSON";
	}
	
	public void prepareRexcuteAllError() {
		try {
			List<ETL_ErrorLog> errorLogs = errorLogService.findList("from ETL_ErrorLog el where el.excel.excel_id = " + excelId);
			ETL_Excel excel = null;
			if (errorLogs != null && errorLogs.size() > 0) {
				Integer[] indexs = new Integer[errorLogs.size()];
				for (Integer i = 0 ; i < errorLogs.size() ; i++) {
					indexs[i] = errorLogs.get(i).getExcelColumn_Index();
				}
				excel = errorLogs.get(0).getExcel();
				Integer error = importService.saveData(excel, indexs);
				String msg = "";
				switch (error) {
				case -99 : msg = "文件不存在，请重新上传后再执行";break;
				case -1 : msg = "导入时发生异常，请稍后再试";break;
				default : msg = "共提取" + indexs.length + "条记录，其中失败" + result + "条记录";
				}
				errorLogService.processIgnoreAllErrors(excelId , 1);
				result.put("msg", msg);
				result.put("success", true);
			}
		} catch (Exception e) {
			result.put("message", "操作失败!");
			result.put("success", false);
			e.printStackTrace();
		}
	}
	
	
	
	public String excelError() {
		return "dataJSON";
	}
	
	public void prepareExcelError(){
		List<ETL_ErrorLog> list = errorLogService.findList("from ETL_ErrorLog where excel.excel_id = " + excelId , page, rows);
		for (ETL_ErrorLog errorLog : list) {
			ErrorLogVO vo = new ErrorLogVO();
			BeanUtils.copyProperties(errorLog, vo);
			vo.setExcel_name(errorLog.getExcel().getExcel_name());
			vo.setExcel_uploadTime(errorLog.getExcel().getExcel_uploadTime());
			vo.setExcelId(errorLog.getExcel().getExcel_id());
			errorList.add(vo);
		}
		this.total = errorLogService.findCountBySql("from ETL_ErrorLog where excel.excel_id = " + excelId);
	}
	
	
	public String ignoreAllError(){
		return "updateJSON";
	}
	
	public void prepareIgnoreAllError() {
		boolean isSuccess = errorLogService.processIgnoreAllErrors(excelId , 2);
		if (isSuccess) {
			result.put("msg", "操作成功!");
			result.put("success", true);
		} else {
			result.put("msg", "操作失败!");
			result.put("success", false);
		}
	}
	public String ignoreError(){
		return "updateJSON";
	}
	
	public void prepareIgnoreError(){
		ETL_ErrorLog errorLog = errorLogService.findById(errorLogId);
		errorLog.setStatus(2);
		boolean isSuccess = errorLogService.alter(errorLog);
		if (isSuccess) {
			result.put("message", "操作成功!");
			result.put("success", true);
		} else {
			result.put("message", "操作失败!");
			result.put("success", false);
		}
	}
	
	public void setExcelId(Integer excelId) {
		this.excelId = excelId;
	}

	public Integer getExcelId() {
		return excelId;
	}

	@JSON(name = "rows")
	public List<ErrorLogVO> getErrorList() {
		return errorList;
	}
	
	public void setErrorLogId(Integer errorLogId) {
		this.errorLogId = errorLogId;
	}
	private Integer total = 0;

	public Integer getTotal() {
		return this.total;
	}
}
